<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\ArtistRankRepositoryEloquent;
use App\Repositories\ArtistProductionRepositoryEloquent;
header("Access-Control-Allow-Origin:*");
/*星号表示所有的域都可以接受，*/
header("Access-Control-Allow-Methods:GET,POST");
class ArtistController extends Controller
{

    /**
     * @var ArtistRankRepositoryEloquent
     */
    protected $artist_repository;
    protected $artist_production_repository;

    public function __construct(ArtistRankRepositoryEloquent $repository, ArtistProductionRepositoryEloquent $production){
        $this->artist_repository = $repository;
        $this->artist_production_repository = $production;
    }
    /**
     * 女优排行榜
     * @author   ljx
     * @date 2017-08-28
     * @return json
     *
     **/
    public function rank() {
        $data = $this->artist_repository->orderBy('id','desc')->first();
        if(!empty($data)) {
            return response()->json([
                'code' => 1,
                'data' => [
                    'date' => $data->id,
                    'rank' => json_decode($data->rank,true)
                ]
            ]);
        }else {
            return response()->json([
                'code' => -1,
                'msg' => '暂无数据'
            ]);
        }
    }
    /**
     * 作品集
     * @author   ljx
     * @date 2017-08-28
     * @param string $artist  女优名
     * @return null
     *
     **/
    public function production($artist) {
        $data = $this->artist_production_repository->findWhere(['artist' => $artist]);
        if(!$data->isEmpty()) {
            $repsone_data = [];
            foreach ($data as $value) {
                $repsone_data['artist'] = $value->artist;
                $repsone_data['last_production'] = json_decode($value->last_production,true);
            }
            return response()->json([
                'code' => 1,
                'data' => $repsone_data
            ]);
        }else {
            return response()->json([
                'code' => -1,
                'msg' => '暂无数据'
            ]);
        }
    }
}
